package com.psak.cudaphrasal.server.service.chunk;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.psak.cudaphrasal.model.FileChunkToProcess;

/**
 * Usluga tworzaca chunki, ktore zostana wyslane do aplikacji klienckich.
 * 
 * @author piotrs10
 * 
 */
public class ChunkCreatorService {
	private static int CHUNK_SIZE = 1000; // pojedyczy chunk zklada sie z 1000
											// slow

	/**
	 * Tworzy chunki na podstawie listy hashy slow.
	 * 
	 * @param wordHashList
	 *            Lista hashy slow.
	 * @return Lista chunkow.
	 */
	public List<FileChunkToProcess> createChunks(List<Long> wordHashList) {
		List<FileChunkToProcess> chunks = new ArrayList<FileChunkToProcess>();
		int chunkStart = 0;
		int chunkEnd = CHUNK_SIZE;
		final int listSize = wordHashList.size();
		while (chunkStart <= listSize - 1) {
			FileChunkToProcess fileChunkToProcess = new FileChunkToProcess();
			if (chunkEnd > wordHashList.size() - 1) {
				// ostatni chunk mniejszy niz CHUNK_SIZE
				fileChunkToProcess.setChunkWordHashList(new LinkedList<Long>(
						wordHashList.subList(chunkStart, wordHashList.size())));
			} else {
				fileChunkToProcess.setChunkWordHashList(new LinkedList<Long>(
						wordHashList.subList(chunkStart, chunkEnd)));
			}
			chunks.add(fileChunkToProcess);
			chunkStart = chunkEnd;
			chunkEnd = chunkEnd + CHUNK_SIZE;
		}
		return chunks;
	}
}
